package com.music.controller.music;

import com.alibaba.fastjson.JSONObject;
import com.music.dto.collection.CollectionPlaylistDTO;
import com.music.dto.system.TokenDTO;
import com.music.dto.utils.OpResultDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.service.music.CollectionPlaylistService;
import com.music.utils.HttpRequestReader;
import com.music.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/collectionPlaylist")
public class CollectionPlaylistController {

    @Resource
    private CollectionPlaylistService collectionPlaylistService;
    private final Logger logger = LoggerFactory.getLogger(PlaylistController.class);

    /**
     * @Description: 获取数据列表
     * @Author hbh
     * @Date 2021/11/26
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = collectionPlaylistService.list4Table(tokenDTO.getAccountId(), tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 添加收藏
     * @Author hbh
     * @Date 2021/11/26
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token,
                           @RequestBody CollectionPlaylistDTO collectionPlaylistDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage(collectionPlaylistService.add(tokenDTO.getAccountId(), collectionPlaylistDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 取消收藏
     * @Author hbh
     * @Date 2021/12/5
     */
    @RequestMapping(value = "/removeByUserId", method = RequestMethod.POST)
    public OpResultDTO removeByUserId(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) throws Exception {
        OpResultDTO opResult = new OpResultDTO();
        JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
        Long playlistId = Long.parseLong(jsonObject.getString("playlistId"));
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage(collectionPlaylistService.removeByUserId(tokenDTO.getAccountId(), playlistId).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

}
